import {
    ShopAdjoin
} from "./adjoin.js"

import {
    Graph
} from "./graph.js"


import {
    Spu
} from "../../model/spu.js"
Page({
    data: {
        product: {},
        specsS: [],
        optionSpecs: [],
        commoditySpecs: [],
        shopAdjoin: null,
        selected: [],
        pIndex: 0
    },
    async onLoad() {
        const product = await Spu.getDetail(2)
        this.setData({
            product
        })

        this.init(product)
    },

    init(product) {
        console.time("INIT-TIME")
        const data = product.sku_list
        const pIndex = product.default_sku_id
        const productSku = data[pIndex]
        const specsS = Array.from({
            length: productSku.specs.length
        })
        const commoditySpecs = Array.from({
            length: productSku.specs.length
        })

        const selected = []
        // console.log(data, 123)
        data.map((item) => {
            item.specs.map(({
                key_id,
                key,
                value_id,
                value
            }, index) => {
                let list = {
                    value_id: value_id,
                    value: value,
                    select: false,
                    disable: true
                }
                if (!commoditySpecs[index]) {
                    selected.push(key)
                    commoditySpecs[index] = {
                        key_id: key_id,
                        key: key,
                        list: [list]
                    }
                }
                if (this.container(commoditySpecs[index].list, list) === -1) commoditySpecs[index].list.push(list)
            })
        })
        const shopAdjoin = new ShopAdjoin(commoditySpecs, data)
        // const shopAdjoin = new Graph(commoditySpecs, data)


        this.setData({
            commoditySpecs,
            product,
            shopAdjoin,
            productSku,
            selected,
            specsS,
            pIndex
        })
        console.timeEnd("INIT-TIME")

    },
    // 判断一个数组中是否某个元素
    container(vert, id) {
        let tem = -1
        vert.forEach((item, index) => {
            if (item.value_id === id.value_id) tem = index
        })
        return tem
    },



    selectTap(event) {
        console.time("time")
        let lists = this.data.commoditySpecs
        let specsS = this.data.specsS
        let pIndex = this.data.pIndex
        let selected = []
        const name = event.detail.name
        const cell = event.detail.cell

        // 禁用不可选项
        if (!cell.disable) return

        // 已选中选项集合
        if (!specsS[name]) {
            specsS[name] = cell
        } else {
            specsS[name] = specsS[name].value_id === cell.value_id ? null : cell;
        }

        // 选项状态更改
        lists[name].list.map(({
            value_id,
            value,
            select,
            disable
        }, index) => {
            // 一行中只能有一个被选中
            if (value_id === cell.value_id) {
                lists[name].list[index].select = !select
            } else {
                lists[name].list[index].select = false
            }
        })

        // 获得可选项表
        const data = this.data.shopAdjoin.querySpecsOptions(specsS)

        lists.map(({
            key_id,
            key,
            list
        }, index) => {
            // 如果没选中，则加进提示数组
            // if (!specsS[index]) selected.push(key)

            list.map(({
                value_id,
                value,
                select,
                disable
            }, i) => {
                // 将不在可选项表中的选项禁用
                list[i].disable = this.container(data, list[i]) > -1
            })
        })
        console.log(lists, 123)

        // 显示选择商品
        // if (selected.length === 0) {
        //     selected = specsS.map((item) => item.value)
        //     pIndex = this.data.shopAdjoin.querySpecs(selected)
        // }

        // const productSku = this.data.product.sku_list[pIndex]
        this.setData({
            commoditySpecs: lists,
            // specsS,
            // selected,
            // productSku
        })
        console.timeEnd("time")
    },

})